//
//  ZZBeautyBigFansVC.m
//  新项目
//
//  Created by liGordon on 17/3/13.
//  Copyright © 2017年 刘耀宗. All rights reserved.
//

#import "ZZBeautyBigFansVC.h"
#import "ZZBeautyBigFansCell.h"
#import "ZZBeautyBigFansDetailVC.h"
#import "ZZEnterpriseBasicInfoViewModel.h"
#import "ZZCustomViewModel.h"
#import "ZZBeautyBigFansContentModel.h"
#import "ZZBeautyBigFansModel.h"
#import "ZZBeautyBigFansSelectedView.h"
static NSString *identifier = @"BeautyBigFansCell";
@interface ZZBeautyBigFansVC ()<UITableViewDataSource,UITableViewDelegate,ZZBeautyBigFansCellDelegate,ZZBeautyBigFansSelectedViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *menuTable;
@property (nonatomic, strong) CcNoDataView *noDataView;
@property (nonatomic, assign) NSInteger total;
@property (nonatomic, assign) NSInteger index;
@property (nonatomic, assign) NSInteger num;
@property (nonatomic,strong)NSMutableArray *dataArr;
@property (nonatomic, strong)ZZBeautyBigFansSelectedView *TanCView;
@property (nonatomic,strong)NSString *param1Str;
@property (nonatomic,strong)NSString *param2Str;
@property (nonatomic,strong)NSString *startTwiceStr;
@property (nonatomic,strong)NSString *endTwiceStr;
@property (nonatomic,strong)NSString *txtbeginStr;
@property (nonatomic,strong)NSString *txtendStr;
@property (nonatomic,strong)NSString *nameStr;

@end

@implementation ZZBeautyBigFansVC
-(ZZBeautyBigFansSelectedView *)TanCView
{
    if (_TanCView == nil) {
        _TanCView = [ZZBeautyBigFansSelectedView ViewWithXib];
        _TanCView.delegate = self;
    }
    return _TanCView;
}
-(NSMutableArray *)dataArr
{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

-(CcNoDataView *)noDataView
{
    if (_noDataView == nil) {
        _noDataView = [[CcNoDataView alloc] init];
        _noDataView.backgroundColor=[UIColor whiteColor];
    }
    return _noDataView;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTableView];
    self.menuTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.title = @"美容忠实客户";
    self.txtbeginStr = @"";
    self.txtendStr = @"";
    self.nameStr = @"";
 //   self.plateNumStr = @"";
    [self initEnterpriseBasicInfo];
    UIBarButtonItem *item=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"ic_wxcb_01"] style:UIBarButtonItemStylePlain target:self action:@selector(clickForSeletecd)];
    self.navigationItem.rightBarButtonItem=item;
    MJRefreshNormalHeader *header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.index=1;
        
        [self initDataWithPageIndex:[NSString stringWithFormat:@"%ld",self.index]];
        [self.menuTable.mj_footer  resetNoMoreData];
    }];
    self.menuTable.mj_footer=[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        if (self.total>upNum*self.index) {
            self.index++;
            [self initDataWithPageIndex:[NSString stringWithFormat:@"%ld",self.index]];
            
        }
        else
        {
            [self.menuTable.mj_footer endRefreshingWithNoMoreData];
            
        }
        
    }];
    
    
    self.menuTable.mj_header=header;
    [self.menuTable.mj_header beginRefreshing];
    [self.view addSubview:self.TanCView];
    CGRectMake(0, -newheight-64-200, newwidth, newheight);

}
- (void)setupTableView
{
    _menuTable.delegate = self;
    _menuTable.dataSource = self;
    _menuTable.rowHeight = 80;
    [_menuTable registerNib:[UINib nibWithNibName:@"ZZBeautyBigFansCell" bundle:nil] forCellReuseIdentifier:identifier];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZZBeautyBigFansCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.model = _dataArr[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


#pragma mark -查询企业基础信息
-(void)initEnterpriseBasicInfo
{
    NSUserDefaults *loginDefaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *loginDict = [loginDefaults objectForKey:@"loginInfo"];
    [[ZZEnterpriseBasicInfoViewModel shareViewModel]getEnterpriseBasicInfoSettingWithCompanyID:loginDict[@"CompanyID"]  andCompanyFID:loginDict[@"CompanyFID"] andIsinherit:@"0" andType:@"TXSZ1022" andCode:@"WXZSKH" andSuccess:^(NSDictionary *result) {
        if (result) {
            
            NSArray *sucArray = [WHJSON objectFormJsonString:[result objectForKey:@"resultJson"]];
            NSDictionary *sucDict = sucArray.firstObject;
            NSLog(@"%@",sucDict[@"Param1"]);
            self.param1Str = sucDict[@"Param1"];
            self.param2Str = sucDict[@"Param2"];
            if (!self.param1Str) {
                self.param1Str = @"";
                
                
            }
            if (!self.param2Str) {
                self.param2Str = @"";
            }
            
            if (!self.param1Str && !self.param2Str) {
                [self.noDataView dataIsEmpty:self.dataArr withView:self.menuTable andNoDataView:self.noDataView andRect:CGRectMake(0, 130, newwidth, newheight-130) andBtnHiddne:YES];
                self.noDataView.label.text=@"暂无数据";
                
                [self.menuTable reloadData];
            }else{
              [self initDataWithPageIndex:[NSString stringWithFormat:@"%ld",self.index]];            }
            
        }
        
    } andFailer:^{
        [self.menuTable.mj_header endRefreshing];
        [self.noDataView noNetwithView:self.menuTable andNoDataView:self.noDataView andRect:CGRectMake(0, 0, newwidth, newheight) andBtnHiddne:NO];
        //点击重新加载的block
        WEAKSELF
        self.noDataView.myBlock=^{
            [weakSelf.menuTable.mj_header beginRefreshing];
        };
        
    }];
}

#pragma mark -查询车辆信息
-(void)initDataWithPageIndex:(NSString *)pageIndex
{
    NSString *PageIndex = [NSString stringWithFormat:@"%ld",(long)pageIndex];
    NSUserDefaults *loginDefaults = [NSUserDefaults standardUserDefaults];
    NSDictionary   *loginDict = [loginDefaults objectForKey:@"loginInfo"];
    
    
    // loginDict[@"CompanyID"]
    [[ZZCustomViewModel shareViewModel]getBeautyOrRepairBigFansWithCompanyID:loginDict[@"CompanyID"] andPageIndex:pageIndex andPageSize:@(upNum) andParam1:self.param1Str andParam2:self.param2Str andType:@"2" andTxtbegin:self.startTwiceStr andTxtEnd:self.endTwiceStr andName:self.nameStr andPlateNumber:@"" andSuccess:^(NSDictionary *result) {
        if (result) {
            
            
            
            
            [ZZBeautyBigFansModel mj_setupObjectClassInArray:^NSDictionary *{
                return @{
                         @"rows" : @"ZZBeautyBigFansContentModel",
                         };
            }];
            ZZBeautyBigFansModel *fg = [ZZBeautyBigFansModel mj_objectWithKeyValues:result[@"resultJson"]];
            
            self.total = [fg.total integerValue];
            
            NSMutableArray *mutableArr=[NSMutableArray array];
            for (NSDictionary *dic in fg.rows) {
                [mutableArr addObject:dic];
            }
            if (self.index!=1) {
                if (mutableArr.count==0) {
                    [self.menuTable.mj_footer endRefreshingWithNoMoreData];
                    return ;
                }
                [self.dataArr addObjectsFromArray: mutableArr];
                [self.menuTable.mj_footer endRefreshing];
                
            }
            else
            {
                self.dataArr=mutableArr;
                [self.menuTable.mj_header endRefreshing];
                
            }
            
            
            
            
            
            
            
            [self.noDataView dataIsEmpty:self.dataArr withView:self.menuTable andNoDataView:self.noDataView andRect:CGRectMake(0, 130, newwidth, newheight-130) andBtnHiddne:YES];
            self.noDataView.label.text=@"暂无数据";
            
            [self.menuTable reloadData];
            
            
        }
        
        
    } andFailer:^{
        [self.menuTable.mj_header endRefreshing];
        [self.noDataView noNetwithView:self.menuTable andNoDataView:self.noDataView andRect:CGRectMake(0, 0, newwidth, newheight) andBtnHiddne:NO];
        //点击重新加载的block
        WEAKSELF
        self.noDataView.myBlock=^{
            [weakSelf.menuTable.mj_header beginRefreshing];
        };
        
    }];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    ZZBeautyBigFansDetailVC *vc = [[ZZBeautyBigFansDetailVC alloc]init];
    vc.model = self.dataArr[indexPath.row];
    PushVC(vc)
    
}

-(void)clickDetail:(ZZBeautyBigFansContentModel *)model
{
    ZZBeautyBigFansDetailVC *vc = [[ZZBeautyBigFansDetailVC alloc]init];
    vc.model = model;
    PushVC(vc)
}

//点击查询
-(void)clickForSeletecd
{
    [UIView animateWithDuration:0.25 animations:^{
        self.TanCView.frame=CGRectMake(0, 64, newwidth, newheight-64);
    }];
    
    
    
}

-(void)clickForSeletectedDataWithSTwice:(NSString *)startTwice andETwice:(NSString *)endTwice andName:(NSString *)name
{
    [self hiddenTheView];
    self.startTwiceStr = startTwice;
    self.endTwiceStr = endTwice;
    self.nameStr = name;
    //客户电话号码
    // self.plateNumStr = telPhone;
    [self.menuTable.mj_header beginRefreshing];
}



//关闭
-(void)hiddenTheView
{
    [self.view endEditing:YES];
    [UIView animateWithDuration:0.25 animations:^{
        self.TanCView.frame=CGRectMake(0, -newheight-64-200, newwidth, newheight);
    }];
    
}


@end
